﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HealthManagement.Models;
using HealthManagement.Views;
using PersonalHealth.Data.Model;
using HealthManagement.Common;
using UI.Comm.Controls;

namespace HealthManagement
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : PMWindow
    {
        //是否为外部调用
        bool isExternal = false;

        /// <summary>
        /// 只能被当前工程启动
        /// </summary>
        public MainWindow()
        {
            MessageBox.Show("非法登陆");
            App.Current.Shutdown();
            return;
            InitializeComponent();
            isExternal = false;
            ContentFrame.Content = new Login();
        }

        Action onLogout;
        /// <summary>
        /// 只能被外部工程启动
        /// </summary>
        public MainWindow(Doctor user, Action onLogout)
        {
            InitializeComponent();
            isExternal = true;
            this.onLogout = onLogout;
            AppData.User = user;
            this.loginUserInfo.Visibility = Visibility.Visible;
            this.NameBlock.Text = user.UserName;
            this.initMenu();
            this.itemList.SelectedIndex = 0;
            this.ContentFrame.Content = new Index();
        }

        public void initMenu()
        {
            List<SysNode> nodes = new List<SysNode>();
            //初始化菜单项
            //首页
            nodes.Add(new SysNode() { Title = "系统首页", Path = "Index", IsSelected = true, IconPath = "../Theme/Images/macosxstyle_020.png" });
            //会员管理
            SysNode userManager = new SysNode() { Title = "客户管理", Path = "MeterPanel", IconPath = "../Theme/Images/macosxstyle_046.png" };
            nodes.Add(userManager);

            if (AppData.User.UserType == UserType.Admin)
            {
                //医生管理
                SysNode doctorManager = new SysNode() { Title = "医生管理", Path = "DoctorPanel", IconPath = "../Theme/Images/macosxstyle_041.png" };
                nodes.Add(doctorManager);

                //系统工具
                SysNode sysManager = new SysNode() { Title = "系统管理", Tag = "SysManager", Path = "SystemPanel", IconPath = "../Theme/Images/macosxstyle_038.png" };
                nodes.Add(sysManager);
            }

            itemList.ItemsSource = nodes;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (isExternal)
            {
                if (onLogout != null)
                    onLogout();
            }
            else
            {
                this.loginUserInfo.Visibility = Visibility.Collapsed;
                this.NameBlock.Text = "";
                AppData.User = null;

                ContentFrame.Content = new Login();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var window = new ChangePassword();
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.ShowDialog();
        }

        private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void itemList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                SysNode item = e.AddedItems[0] as SysNode;
                if (item.Path != null && !string.IsNullOrEmpty(item.Path.ToString()))
                {
                    ContentFrame.Source = new Uri("Views/" + item.Path + ".xaml", UriKind.Relative);
                }
            }
        }
    }
}
